<template>
	<main :style="bottomPadding">
		<van-list v-if="footPrintList&&footPrintList.length>0"
				  :finished="finished" v-model:loading="loading" v-model:error="error" error-text="请求失败，点击重新加载">
			<template #finished><van-divider>没有更多了</van-divider></template>
			<template v-for="group in footPrintList" key="group.date">
				<div class="date-line">
					<s/>
					<div class="group-label" :class="operSwitch? 'show-mask':''">
						<div>{{group.date}}</div>
						<div class="font-12 txt-gray"><span class="txt-primary">{{group.offers.length}}</span>件商品</div>
					</div>
				</div>
				<van-row :gutter="8">
					<GoodsFavoritesItem v-for="(item,index) in group.offers" :item="item" key="item.gdsId" :show-mask="operSwitch" @check-change="checkDentity"/>
				</van-row>
			</template>
		</van-list>
		<van-empty v-else image="search" description="您暂无浏览商品的足迹~" />
	</main>
</template>
<style lang="scss" scoped>
.van-row{margin:0 8px}
.date-line{
	height:34px; position:relative;
	.group-label{
		background: var(--body-bg-color); padding:0 8px 0 8px; position: absolute; left:16px;
		font-size:13px;
		.van-icon{position:absolute;right:4px;top:6px;width:20px;height:20px;font-size:20px;display:none;}
		&.show-mask{
			.van-icon{display:block;}
		}
	}
	s{display:block;  position:absolute; top:16px; left:0; right:0; height:1px; background:var(--border-color);}
}
</style>
<script lang="ts" setup>

const selectedNum = inject('selectedNum')
const totalNum = inject('totalNum')
const operSwitch = inject('operSwitch',true)
const checkedAll = inject('checkedAll')

const finished=ref(true)
const loading=ref(false)
const error=ref(false)

//computed
const bottomPadding = computed(()=>{
	if(!operSwitch.value){
		return 'padding-bottom:calc(env(safe-area-inset-bottom))!important'
	}else{
		return 'padding-bottom:calc(50px + env(safe-area-inset-bottom))!important'
	}
})

//data
//浏览足迹列表
const footPrintList = ref(<Array<any>>[])
footPrintList.value = [
	{
		date:'2023-10-01',
		offers:[
			{
				gdsId:'1254260',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				isFollowed:true,
			},
			{
				gdsId:'1227861',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'0', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1227862',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'2', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
		],
	},
	{
		date:'2023-10-02',
		offers:[
			{
				gdsId:'1227863',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				oldPrice:630000,
			},
			{
				gdsId:'1254264',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				oldPrice:425000,
			},
			{
				gdsId:'1254265',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1254266',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				isFollowed:true,
			},
			{
				gdsId:'1227867',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1227868',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				isFollowed:true,
			},
			{
				gdsId:'1227869',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1254270',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
				agentPrice:43000,
				guidePrice:48000,
				shopId:'ABRL4321',
				shopType:'厂家自营店',
			},
			{
				gdsId:'1254271',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1254272',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1227873',
				gdsImg:'https://www.woego.cn/woego/image/622aaf5a75e50b69f7a2b40d_140x140!.jpg',
				gdsName:'Apple iPhone 13 Pro Max',
				agentPrice:530000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
		]
	},
	{
		date:'2023-10-04',
		offers:[
			{
				gdsId:'1254274',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生 小象智能家庭医生 小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
			},
			{
				gdsId:'1254275',
				gdsImg:'https:www.woego.cn/woego/image/6449d39e2b608d0a734b9585_140x140!.jpg',
				gdsName:'小象智能家庭医生',
				agentPrice:43000,
				gdsStatus:'1', //1有效 0已下架 2无货
				shopId:'ABRL4321',
				isFollowed:true,
			},
		],
	},
]

//先假设已经读取了后台数据，返回了totalNum (后面要改的)
for(let tmp of footPrintList.value){
	totalNum.value+=tmp.offers.length
}

//methods
const checkDentity=()=>{
	let allCheck=true,selNum=0
	for(let tmp of footPrintList.value){
		for(let it of tmp.offers){
			if(!it.checked){
				allCheck=false
			}else{
				++selNum
			}
		}
	}
	selectedNum.value=selNum
	checkedAll.value=allCheck
}

const toggleAll=(val:boolean)=>{
	let selNum=0
	for(let tmp of footPrintList.value){
		for(let it of tmp.offers){
			it.checked=val
			if(tmp.checked)	++selNum
		}
	}
	selectedNum.value=selNum
	checkedAll.value=val
}

const deleteBatch=()=>{
	let len = 0, delList:string[]=[]
	for(let group of footPrintList.value){
		for(let item of group.offers){
			if(item.checked){
				++len
				delList.push(item.gdsId)
			}
		}
	}
	console.log(delList)
	return delList
}
defineExpose({
	toggleAll,deleteBatch
})
</script>